#include "InputWatcher.h"

#include <stdlib.h>

#include "worker-monitor-topics.h"
#include "SatelliteInterface.h"

const int InputWatcher::lineInput = 19;
const int InputWatcher::INPUTSIZE=9;

InputWatcher::InputWatcher(){}
InputWatcher::~InputWatcher() {}
//InputWatcher::InputWatcher(Gui* sing){ this.Singleton=sing}

void InputWatcher::init() {

	input[INPUTSIZE];//char InputWatcher::input[INPUTSIZE];
	i=0;
	PRINTF("inited INININNINININININININITIEEEAAART!biugsgrdiuhbgfdiluhounhruhrbsdiunh");
	InputWatcher::singleton=Gui::getSingletonPtr();
}


void InputWatcher::run() {
	singleton->setReply("RUNRUNRUNRUNRUNRRUNRUUUUUUUUUUUUUUNNNN!!!!!");
	bool reading=true;
	int i=0;
	bool stop=false;

	while(1) {

		singleton->refresh();

		PRINTF(SCREEN_MOVE_CURSOR_TO,11,1);

		//Eingabeüberwachung

		PRINTF(SCREEN_MOVE_CURSOR_TO,lineInput,i+1);
			if(reading){
				stop=false;
				PRINTF("start reading");
				while(isCharReady()&&reading&&!stop){
					char buff= getchar();
					if(buff=='\n')
						reading=false;
					else
					if(buff>47&&buff<58 || buff>64&&buff<91 || buff>96&&buff<123)
					{
						input[i]=buff;//PRINTF("charisready  i= %ld\n",i);
						i++;
					}
					else stop=true;

					if(!stop&&i==INPUTSIZE)
						stop=true;
				}
				PRINTF(SCREEN_MOVE_CURSOR_TO, lineInput+2);
				PRINTF("while schleife has stopped!");

				//singleton->setConsoleInput(input);
			}

			if(!reading){
				singleton->setInfoMessage(input);
				handleInput();
				PRINTF("entered:  !%s!",input);

				//reset i,input,reading,consoleInput
				for(int j=0;j<INPUTSIZE;j++) input[j]='\0';
				i=0;
				reading=true;
				singleton->setConsoleInput("reseted");
			}

	}
}

void InputWatcher::handleInput(){

	WMMessage msg;
	bool unknownCommand=false;
	switch(i){
	case 1:
		if(input[0]=='p'){singleton->setReply("Sending PING!");
			msg.messageType=PING;}
		else if(input[0]=='i')singleton->setReply("printing Info!");
		else unknownCommand=true;
		break;
	case 2:
		if( (input[0]=='i') && (input[1]=='w'))
			msg.messageType= identifyWorker;
		else if( (input[0]=='s') && (input[1]=='w'))
			singleton->setReply("Missing Argument: ID of InputWatcher to become Worker");
		else if( (input[0]=='s') && (input[1]=='m'))
			singleton->setReply("Missing Argument: Mode to switch into");
		else unknownCommand=true;
		break;
	case 3:
		if( input[0]=='s' && input[1]=='w' /* PRÜFE AUF ZAHL BEI input[2]*/){
			msg.messageType= SetWorker;
			msg.nodeNumber=(int) input[2]/*umgecastet*/;
		}
		else if( input[0]=='s' && input[1]=='m'/* Prüfe auf gültigen Modus*/){
			msg.messageType=SetMode;
			//msg.genData=..
		}
		else if(input[0]=='s'&&input[1]=='s'&&input[2]=='p')
			singleton->setReply("Missing Argument: Time in ms to set Status Period to");
		else unknownCommand=true;
		break;
	case 4:
	case 5:
	case 6:
	case 7:
	case 8:
	case 9:
		if(input[0]=='s'&&input[1]=='s'&&input[2]=='p')
		{	int num=0;
			bool allNum=true;
			for(int j=3;j++;j<i)
			{
				if(47<input[j]<58)
					num=num*10+input[j];
				else allNum=false; // =========================fehlermeldung bei false sonst msg erstellen
			}
		}
	default: unknownCommand=true;break;
	}
	
	if(unknownCommand) singleton->setReply("Unknown command, type i to check valid commands.");
	singleton->refresh();

			//länger als 3 zeichen fehlend

			//erzeugte MSG rausschicken.

}


//waitUntil(NOW()+5000*MILLISECONDS);

/**
 * Class used to trigger the TimeEvents
 * same as in Node.cpp
 */

/*
class TimeEventPropagator : public Thread
{
public:
	TimeEventPropagator() : Thread("TimeEventPropagation"){}
	/**
	 * Triggers the TimeEvents every 50 Milliseconds
	 */
/*	void run(){
		TIME_LOOP(NOW(),50*MILLISECONDS) {
			TimeEvent::propagate(NOW());
		}
	}
};

static TimeEventPropagator TEP;
*/
